<?php
namespace App\Logic\SalesReportLT;

use App\Models\Basics\ExchangeRate;
use App\Models\SalesReportLT\PaymentClassify as TPaymentClassify;
use App\Models\SalesReportLT\PaymentDetail as TPaymentDetail;
use Illuminate\Support\Facades\DB;
class PaymentClassify
{
    public static function exePayment($date){
        ini_set('memory_limit', '200M');
        set_time_limit(0);
        try{
            $start = time();
            $start_memory = memory_get_usage();
            DB::beginTransaction();
            $headers = TPaymentDetail::headersField();
            $groupByArr = ['classify_id','seller_id'];
            $pc = TPaymentDetail::query()
                ->where('report_date',$date)
                ->selectRaw(TPaymentClassify::_getFields($headers,$groupByArr))
                ->groupBy($groupByArr)
                ->get();
            $rateOdds = ExchangeRate::getRateOddsToRMB('JPY',strtotime($date));
            foreach ($pc as $pd){
                $data = TPaymentClassify::_getData($rateOdds,$pd);
                TPaymentClassify::query()->updateOrCreate(
                    ['report_date' => $date,'classify_id' => $pd->classify_id,'seller_id' => $pd->seller_id], $data
                );
            }
            DB::commit();
            $end = time();
            $end_memory = memory_get_usage();
            print_r(($end - $start).'--------'.(($end_memory - $start_memory)/1024/1024));
        }catch (\Exception $e){
            DB::rollBack();
            report($e);
        }
    }
}
